#include "rrCintamaniManager.h"
#include "rrCintamaniDOM.h"
#include "rrCintamaniDocument.h"
#include "rrCintamaniUrl.h"
#include "rrCintamaniPanel.h"
#include "JSCallback.h"

Panel::Panel( rrCintamaniManager * mManager, rrCintamaniElementPtr feedReader,  OpmlInfo & feeds ) : man(mManager)
{
	static int count = 0;
	rrCintamaniWindowPtr Window =	mManager->GetWindow();
	rrCintamaniDocumentPtr Document = Window->GetDocument();

	static int id = 0;
	std::ostringstream IdStr;
	IdStr << ( id++ ) ;
	// colours
	count = (count + 1) % 4;
	std::ostringstream CountStr;
	CountStr << ( count + 1) ;

	feedPanel = Document->CreateElement("div");
	feedPanel->SetClassName("feedPanel pan"+CountStr.str());
	feedPanel->SetId(IdStr.str());
	feedPanel->GetStyle()->SetDisplay("block");
	feedPanel->GetStyle()->SetWidth("200px");
	feedPanel->AddEventListener( "click",  FB::make_callback( feedPanel, &rrCintamaniElement::OnClick)  );
	feedPanel->AddListener(this);
	feedReader->AppendChild(feedPanel);

	feedHeader = Document->CreateElement("div");
	feedHeader->SetClassName("feedHeader");
	feedHeader->SetInnerHtml(feeds.title);
	feedHeader->AddEventListener( "click",  FB::make_callback( feedHeader, &rrCintamaniElement::OnClick)  );
	 
	feedHeader->AddListener(this);
	feedPanel->AppendChild(feedHeader);

	rrCintamaniElementPtr feedContent = Document->CreateElement("div");
	feedContent->SetClassName("feedContent");
	feedContent->SetAttribute("name",feeds.xmlUrl);

	feedPanel->AppendChild(feedContent);
	Document->GetBody()->AppendChild( feedReader );

	/////////////

	std::vector<RssItem> ItemList;
	if(!mManager->GetRssFeed( feeds.xmlUrl, ItemList )) {
		mManager->GetHost()->htmlLog("Rss feed unavailable : " + feeds.xmlUrl);
	} 

	for(int y = 0 ; y < ItemList.size(); y++ ) {

		rrCintamaniElementPtr entry = Document->CreateElement("div");
		entry->SetClassName("entry");

		rrCintamaniElementPtr H2 = Document->CreateElement("H2");
		H2->SetClassName("postTitle");
		H2->SetInnerHtml( ItemList[y].title );
		entry->AppendChild(H2);

		rrCintamaniElementPtr p = Document->CreateElement("p");
		p->SetClassName("description");
		p->SetInnerHtml( ItemList[y].description );
		entry->AppendChild(p);


		static int TestVideoNum = 0;

		if( TestVideoNum < 5 ){
			rrCintamaniElementVideoPtr VideoElement = Document->CreateElementVideo("div" );
			VideoElement->SetAttribute( "name" , "mydivclicked");
			VideoElement->SetAttribute("width","100%");
			VideoElement->SetAttribute("height","100%");

			//FB::JSAPIPtr pt = FB::make_callback( VideoElement, &rrCintamaniElementVideo::OnClick) ;
			//VideoElement->AddEventListener("mouseover",pt);

			//FB::JSAPIPtr pt1 = FB::make_callback( VideoElement, &rrCintamaniElementVideo::OnResize) ;
			//VideoElement->AddEventListener("mousedown",pt1);
			
			VideoElement->Initialize("http://pseudo01.hddn.com/vod/demo.flowplayervod/flowplayer-700.flv");
			entry->AppendChild(VideoElement);
		}

		TestVideoNum++;


		if( ItemList[y].img ) {
			rrCintamaniElementPtr img = Document->CreateElement("img");
			img->SetClassName("");
			img->SetAttribute("src",ItemList[y].img->src);
			img->SetAttribute("alt",ItemList[y].img->alt);
			img->SetAttribute("width","100%");
			//img->SetAttribute("height","100");
			entry->AppendChild(img);
		}

		rrCintamaniElementPtr hr = Document->CreateElement("hr");
		entry->AppendChild(hr);
		feedContent->AppendChild(entry);
	}
}

Panel::~Panel()
{

} 

void Panel::OnClick( rrCintamaniElement * Element )
{
	if(feedHeader.get() == Element ){
		feedHeader->GetStyle()->SetBackground("red");
		//man->GetWindow()->Alert("Clicked Header !");
	}
	else if( feedPanel.get() == Element )
	{
		//man->GetWindow()->Alert("Clicked Panel !");
		static bool dir = true;
		if(dir){
			man->GetHost()->evaluateJavaScript("$('#"+feedPanel->GetId()+"').animate({ width : 500 + 'px' }, 1000);");
		}else{
			man->GetHost()->evaluateJavaScript("$('#"+feedPanel->GetId()+"').animate({ width : 200 + 'px' }, 1000);");
		}

		dir = !dir;
	}
} 